﻿using ChuanYe.Core.Utils;
using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class MaplayerBLL
    {
        #region 构造单例
        private MaplayerBLL() { }
        private static MaplayerBLL _instance;
        public static MaplayerBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new MaplayerBLL());
            }
        }
        #endregion

        public int Add(Maplayer info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                ef.Maplayer.Add(info);
                ef.Entry<Maplayer>(info).State = EntityState.Added;
                return ef.SaveChanges();
            }
        }

        public int Remove(int layerId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Maplayer.Where(p => p.LayerId == layerId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {
                    ef.Maplayer.Add(exist);
                    ef.Entry<Maplayer>(exist).State = EntityState.Deleted;
                    return ef.SaveChanges();
                }
            }
        }

        public int Update(Maplayer info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Maplayer.Where(p => p.LayerId == info.LayerId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {
                    exist.LayerName = info.LayerName;
                    exist.LayerData = info.LayerData;
                    ef.Maplayer.Add(exist);
                    ef.Entry<Maplayer>(exist).State = EntityState.Modified;
                    return ef.SaveChanges();
                }

            }
        }

        public Maplayer GetLinchpin(int layerId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Maplayer.Where(p => p.LayerId == layerId).FirstOrDefault();
            }
        }


        public ResultDto<dynamic, dynamic> GetByPage(RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            var sql = AppSetting.GetConfig("Sql:BasicSelect");
            sql = string.Format(sql, "maplayer");
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                string paramsSql = string.Empty;

                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    paramsSql += page.Keyword.KeywordSql("LayerName");
                }
                paramsSql += page.OrderByPattern == 0 ? "LayerID".OrderByDescending() : "LayerID".OrderBy();
                paramsSql += page.Limit();
                sql += paramsSql;
                var countSql = sql.CountSql();
                var list = ef.Database.SqlQuery<Maplayer>(sql);
                result.FirstParam = list;
                result.SecondParam = ef.Database.SqlQuery(countSql).Rows[0][0];
            }
            return result;
        }








    }
}
